package com.itheima.d1_char_stream;

import java.io.*;

public class Test {
    public static void main(String[] args) {
        // 使用字符流复制文本文件
        String sourcePath = "loo.txt";
        String targetPath = "copyloo.txt";
        try {
            copyFile(sourcePath, targetPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void copyFile(String sourcePath, String targetPath) throws IOException {
        // 1. 创建转换流
        final FileReader fr = new FileReader(sourcePath);
        final FileWriter fw = new FileWriter(targetPath);

        // 2. 写入数据
        int len = -1;
        char[] carr = new char[1024];

        while ((len = fr.read(carr)) != -1) {
            final String str = new String(carr, 0, len);
            System.out.println(str);
            // 得到的数据写入目标文件
            fw.write(str);
        }

        // 3. 关闭流
        fr.close();
        fw.close();
    }
}
